package com.bps.zzant.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.bps.zzant.model.OrderGarment;
import com.bps.zzant.model.ToDo;

@Repository
public class OrderDAOImpl implements OrderDAO {
	
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void updateGarmentOrder( OrderGarment order){
		// getSessionFactory().getCurrentSession().update(order);
		getSessionFactory().getCurrentSession().merge(order);
		getSessionFactory().getCurrentSession().flush();
		
	}
	public void deleteGarmentOrder(OrderGarment order){
		
	}
	public void saveGarmentOrder(OrderGarment order){
		getSessionFactory().getCurrentSession().save(order);
		getSessionFactory().getCurrentSession().flush();
		//System.out.println("-------before flush id: " + order.getOrderId());
		//getSessionFactory().getCurrentSession().flush();
		// System.out.println("-------after flush id: " + order.getOrderId());
	}
	public OrderGarment getOrderById(int orderId){
		System.out.println("--------------- in the OrderDAOImpl.getOrderById, orderId="+orderId);
		return (OrderGarment) getSessionFactory().getCurrentSession().get(OrderGarment.class, new Integer(orderId));
	}
	
	public List<OrderGarment> getOrderList(int customerId, String ordersStatus) {
		
		 // List list = getSessionFactory().getCurrentSession().createQuery("from ToDo todo where todo.owner='"+owner +"' and todo.status='"+status+"'").list();
		
		List list = getSessionFactory().getCurrentSession().createCriteria(OrderGarment.class)
				.add(Restrictions.eq("customerId", customerId))
				.add(Restrictions.eq("ordersStatus", ordersStatus))
				.list();
		
		 System.out.println("-----------OrderDAOImpl.getOrderList--- the list size:" + list.size());
		return list;
	}
	
	public List<OrderGarment> getOrderList(String ordersStatus, Date startingDate, Date endDate) {
		
		 List list = getSessionFactory().getCurrentSession().createCriteria(OrderGarment.class)
				.add(Restrictions.between("estPickupDate", startingDate, endDate))
				.add(Restrictions.eq("ordersStatus", ordersStatus)).addOrder(Order.asc("estPickupDate"))
				.list();
		
		 System.out.println("-----------OrderDAOImpl.getOrderList(orderStatus, startingDate, endDate)---<"+ordersStatus+"> the list size:" + list.size());
		return list;
	}

	public List<OrderGarment> getOrderList(String customerName, String ordersStatus, Date startingDate, Date endDate) {
		
		 List list = getSessionFactory().getCurrentSession().createCriteria(OrderGarment.class)
				.add(Restrictions.between("estPickupDate", startingDate, endDate))
				.add(Restrictions.eq("ordersStatus", ordersStatus))
				.add(Restrictions.like("customerName", customerName)).addOrder(Order.asc("estPickupDate"))
				.list();
		
		 System.out.println("-----------OrderDAOImpl.getOrderList(customerName, orderStatus, startingDate, endDate)---<"+ordersStatus+"> the list size:" + list.size());
		return list;
	}
	
	public List<OrderGarment> getOrderList(String customerName, String ordersStatus) {
		
		 List<OrderGarment> list = getSessionFactory().getCurrentSession().createCriteria(OrderGarment.class)
				.add(Restrictions.eq("ordersStatus", ordersStatus))
				.add(Restrictions.like("customerName", customerName)).addOrder(Order.asc("estPickupDate"))
				.list();
				 
		return list;
	}
	
	public List<OrderGarment> getOrderList(String customerName) {
		
		 List<OrderGarment> list = getSessionFactory().getCurrentSession().createCriteria(OrderGarment.class)
				.add(Restrictions.like("customerName", customerName)).addOrder(Order.asc("estPickupDate"))
				.list();
				 
		return list;
	}
}
